package huoye.shops.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.codeideology.android.widget.swiperefresh.AXIRefreshLayout;

import java.util.List;

import huoye.shops.R;
import huoye.shops.utils.PullToReshHelper;


public abstract class PullToRefreshActivity<E extends MyPojo> extends BaseActivity implements RecyclerViewAdapter.OnItemClickListener<E>, PullToReshHelper.OnRefreshListener {

	protected AXIRefreshLayout mRefreshLayout;

	protected RecyclerView mRecycler;

	protected View emptyView;
	protected TextView emptyValue;

	protected RecyclerViewAdapter mAdapter;
	public abstract RecyclerViewAdapter createAdapter();

	protected PullToReshHelper<RecyclerViewAdapter, E> pullToReshHelper;

	protected long currentTime;

	@Override
	protected void initView(@Nullable Bundle bundle) {
		mRefreshLayout = (AXIRefreshLayout) findViewById(R.id.rl_recyclerview_refresh);
		mRecycler = (RecyclerView) findViewById(R.id.list);
		emptyView = findViewById(R.id.empty_list_ll);
//		RecyclerView.ItemDecoration decoration = getItemDecoration();
//		if(decoration != null) {
//			mRecycler.addItemDecoration(getItemDecoration());
//		}
		mAdapter = createAdapter();


		pullToReshHelper = new PullToReshHelper<>(this, mRefreshLayout, mRecycler, this,isAlterManager);
		Log.e("cdcdcdcdc",isAlterManager+"");
		pullToReshHelper.setEmptyView(emptyView);
		pullToReshHelper.setAdapter(mAdapter);
		pullToReshHelper.setLayoutManager(getLayoutManager());
		pullToReshHelper.init();


		mAdapter.setOnItemClickListener(this);
	}

	boolean isAlterManager = false;
	public void setLayoutManager(){
		isAlterManager = true;
	}

//	protected RecyclerView.ItemDecoration getItemDecoration() {
//		HorizontalDividerItemDecoration.Builder builder = new HorizontalDividerItemDecoration.Builder(this);
//		builder.color(R.color.gray_d7)
//				.size(getResources().getDimensionPixelSize(R.dimen.line_height))
//				.showLastDivider();
//		return builder.build();
//	}

	protected RecyclerView.LayoutManager getLayoutManager() {
		return null;
	}

	protected int getContentViewId() {
		return R.layout.pull_to_refresh_activity_layout;
	}

	/**
	 * 查询数据
	 */
	public void query() {
		currentTime = System.currentTimeMillis() / 1000;

		query(new Paging());
	}

	public void processQuery(Paging paging) {
		query(paging);
	}

	public void query(Paging paging) {
		showProcessingIndicator();
	}

	protected void handlerExecption(String msg, String code, Throwable e) {
		pullToReshHelper.validateEmpty();
		hideProcessingIndicator();
	}


	public void setDataSet(List<E> data, Paging paging) {
		pullToReshHelper.setDataset(data, paging);
		hideProcessingIndicator();
	}

	@Override
	public boolean isQueryMore() {
		if(mAdapter == null || mAdapter.getListCount() == 0 || mAdapter.getListCount() < Paging.DEFAULT_PAGE_SIZE) {
			return false;
		}
		return true;
	}

}
